#!/usr/bin/env python
#coding=utf-8
from BeautifulSoup import BeautifulSoup 
import urllib,time

from pysqlite2 import dbapi2 as sqlite
from scode import SCoder

def getdata(soup):
    items = {'open':'开盘', 'close':'最新价','high':'最高价', 'low':'最低价', 
             'volume': '成交量'}
    results = {}
    for k in items.keys():
        u = unicode(items[k], 'u8')                 
        t = soup.find(text= lambda(x): x.find(u) != -1)
        t2 = t.nextSibling.string
        #print t2
        results[k] = t2
    u = unicode('最新价', 'u8')                 
    t = soup.find(text= lambda(x): x.find(u) != -1) 
    d = t.nextSibling.nextSibling.nextSibling.string
    d = d.split('(')[0][:-1]  
    results['change'] = d 
    return results

c = SCoder()
c.set_code('600992')#60726,600992

con = sqlite.connect('/home/jay/prices.db')
cursor = con.cursor()
insert_sql = "insert into prices(code, open, close, high, low, volume,change) values(?,?,?,?,?,?,?)"
i = 0 
while True:
    try:
        time.sleep(1)
        i=i+1
        code = c.next()
        print code
        url = 'http://cn.finance.yahoo.com/q?s=%s.SS'%(code)
        f = urllib.urlopen(url)
        soup = BeautifulSoup(f, fromEncoding='gb18030')
        
        data = getdata(soup)
        data['volume'] = data['volume'][:-3]
        
        v = [code, data['open'], data['close'], data['high'],data['low'], data['volume'],data['change']]
        cursor.execute(insert_sql, v)
        
        if i%10 == 0:
            con.commit()
            print "\ncommit\n"
        
        
        for d in data.keys():
            print d, data[d]
        print '\n'
    except StopIteration : 
        break
    
        

con.commit()
c.close()
con.close()
